package com.mocha.nezha.service;

import cn.hutool.core.collection.CollectionUtil;
import com.mocha.nezha.dmo.entity.CameraPGEntity;
import com.mocha.nezha.dmo.entity.ParkPGEntity;
import com.mocha.nezha.dmo.entity.PolePGEntity;
import com.mocha.nezha.dmo.entity.PolePointPGEntity;
import com.mocha.nezha.nezha.PoleEdge;
import com.mocha.nezha.nezha.PoleVertex;
import com.mocha.nezha.repository.CameraPGRepository;
import com.mocha.nezha.repository.ParkPGRepository;
import com.mocha.nezha.repository.PolePGRepository;
import com.mocha.nezha.repository.PolePointPGRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/3/20 17:13
 */
@Service
@Slf4j
@AllArgsConstructor
public class NezhaService {

    private final PolePGRepository polePGRepository;
    private final PolePointPGRepository polePointPGRepository;
    private final CameraPGRepository cameraPGRepository;
    private final ParkPGRepository parkPGRepository;

    //查询所有
    public Map<String, List<PoleVertex>> mapPoleVertex(){
       List<PolePGEntity>  list = polePGRepository.list();
       if (CollectionUtil.isEmpty(list)){
           return null;
       }
       return list.stream().collect(Collectors.groupingBy( PolePGEntity::getParkCode,
               Collectors.mapping(  PolePGEntity::buildVertex,   Collectors.toList()  ) ));
    }


    public Map<String, List<PoleEdge>>  mapPoleEdg(){
        List<PolePointPGEntity> list = polePointPGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }

        return list.stream().collect(Collectors.groupingBy( PolePointPGEntity::getParkCode,
                Collectors.mapping(  PolePointPGEntity::buildEdge,   Collectors.toList()  ) ));
    }

    public Map<String, Map<String, CameraPGEntity>> mapCamera(){
        List<CameraPGEntity> list = cameraPGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }

        return list.stream().collect(Collectors.groupingBy(CameraPGEntity::getParkCode,
                Collectors.toMap(CameraPGEntity::getCameraCode, Function.identity()) ));
    }

    public  List<String> parkList(){
        List<ParkPGEntity> list = parkPGRepository.list();
        if (CollectionUtil.isEmpty(list)){
            return null;
        }

//        return list.stream().collect(Collectors.toMap(ParkPGEntity::getParkCode, ParkPGEntity::getParkCode));
        return list.stream().map(ParkPGEntity::getParkCode).collect(Collectors.toList());
    }

}
